package com.ys.fmms.config;

import com.ys.fmms.interceptor.HandlerRequestInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {

    @Value("${save-file-path}")
    private String fileSavePath;

    @Value("${save-video-path}")
    private String fileVideoPath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/upload/**")
                .addResourceLocations("file:" + fileSavePath);
        registry.addResourceHandler("/uploadVideo/**")
                .addResourceLocations("file:" + fileVideoPath);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 创建拦截器对象
        HandlerInterceptor handlerRequestInterceptor = new HandlerRequestInterceptor();
        // 指定拦截器的请求uri地址
        String[] path = {"/fmms/**"};
        // 指定不拦截的地址
        String excludePath[] = {"/fmms/systemController/**",
                "/fmms/permissionController/getMenuListByUser/**"
                , "/fmms/dataDisplayController/**",
                "fmms/videoUploadController/**"};
        registry.addInterceptor(handlerRequestInterceptor)
                .addPathPatterns(path)
                .excludePathPatterns(excludePath);
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfig());
        return new CorsFilter(source);
    }

    private CorsConfiguration corsConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedOriginPattern("*");
        // corsConfiguration.setAllowCredentials(true);
        corsConfiguration.setMaxAge(3 * 1200L);
        return corsConfiguration;
    }

    // 配置CORS Cross-Origin Resource Sharing
  /*  public void addCorsMapping(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedHeaders("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE","OPTIONS")
                .allowCredentials(true)
                .maxAge(3 * 1200);
    }*/
}
